package 代码记录.指定公司代码练习记录.备战快手;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author zx
 * @create 2023-05-13 12:07
 */
public class 每日温度 {
    /**
     * @return 单调栈：背
     */
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new ArrayDeque<>();
        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            /**
             * 取出下标进行元素值的比较
             */
            while (!stack.isEmpty() &&
                    temperatures[i] > temperatures[stack.peek()]) {
                int preIndex = stack.pop();
                res[preIndex] = i - preIndex;
            }
            stack.push(i);
        }
        return res;
    }
}
